﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for ChucNangDAL
/// </summary>
public class ChucNangDAL
{
    public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<ChucNangINFO> listAll()
    {
        string sql = "select * from ChucNang";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<ChucNangINFO>(dr);
    }

    public static List<ChucNangINFO> daChucnNangTheLoaiCha(int MaLoaiCha)
    {
        string sql = "select * from ChucNang Where LoaiCha = " + MaLoaiCha;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<ChucNangINFO>(dr);
    }

    public static List<ChucNangINFO> dsChucNangCha(int MaQL)
    {
        string sql = "SELECT * FROM CHUCNANG WHERE MaChucNang IN (";
        sql += "SELECT DISTINCT CHUCNANG.LoaiCha FROM CHUCNANG INNER JOIN CHUCNANG_QUANLY ";
        sql += "ON CHUCNANG.MaChucNang = CHUCNANG_QUANLY.MaChucNang ";
        sql += "WHERE CHUCNANG_QUANLY.MaQL = " + MaQL + " AND CHUCNANG.TrangThai = 1) Order by ThuTu";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<ChucNangINFO>(dr);
    }

    public static List<ChucNangINFO> dsChucNang(int MaQL)
    {
        string sql = "SELECT CHUCNANG.* FROM CHUCNANG INNER JOIN CHUCNANG_QUANLY ";
        sql += "ON CHUCNANG.MaChucNang = CHUCNANG_QUANLY.MaChucNang ";
        sql += "WHERE CHUCNANG_QUANLY.MaQL = '" + MaQL + "' Order by CHUCNANG.LoaiCha,CHUCNANG.ThuTu";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<ChucNangINFO>(dr);
    }

    public static List<ChucNangINFO> listParentByID(int parent)
    {
        string sql = "select * from ChucNang WHERE LoaiCha = " + parent;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<ChucNangINFO>(dr);
    }

    public static ChucNangINFO GetInfo(int MaCN)
    {
        string sql = "select * from ChucNang where MaCN =" + MaCN + "";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<ChucNangINFO>(dr);
    }
}